package com.hw.one.dam2.util;

import java.text.DecimalFormat;

public class AQIProcess {

	public static String[] pollutant = { "细颗粒物(PM2.5)", "颗粒物(PM10)", "二氧化硫(SO2)", "二氧化氮(NO2)", "一氧化碳(CO)", "臭氧1小时" };
	public static String[] pollutantday = { "细颗粒物(PM2.5)", "颗粒物(PM10)", "二氧化硫(SO2)", "二氧化氮(NO2)", "一氧化碳(CO)", "臭氧8小时" };


	static DecimalFormat df = new DecimalFormat("######0.00");
	
	/**
	 * 获取aqi
	 * 
	 * @param pm10value
	 * @param pm25value
	 * @param so2value
	 * @param no2value
	 * @param covalue
	 * @param o3value
	 * @return
	 */
	public static int getAqi(int pm10value, int pm25value, int so2value, int no2value, double covalue, int o3value) {
		int iaqi_pm10 = AQIProcess.cal_pm10_iaqi(pm10value);
		int iaqi_pm25 = AQIProcess.cal_pm25_iaqi(pm25value);
		int iaqi_so2 = AQIProcess.cal_SO2_iaqi(so2value);
		int iaqi_no2 = AQIProcess.cal_NO2_iaqi(no2value);
		int iaqi_co = AQIProcess.cal_CO_iaqi((int) covalue);
		int iaqi_o3 = AQIProcess.cal_O3_iaqi(o3value);

		int a[] = { iaqi_pm25, iaqi_pm10, iaqi_so2, iaqi_no2, iaqi_co, iaqi_o3 };
		int index = max(a);
		int aqi = a[index];
		return aqi;
	}

	public static String getPollution(int pm10value, int pm25value, int so2value, int no2value, double covalue,
			int o3value) {
		int iaqi_pm10 = AQIProcess.cal_pm10_iaqi(pm10value);
		int iaqi_pm25 = AQIProcess.cal_pm25_iaqi(pm25value);
		int iaqi_so2 = AQIProcess.cal_SO2_iaqi(so2value);
		int iaqi_no2 = AQIProcess.cal_NO2_iaqi(no2value);
		int iaqi_co = AQIProcess.cal_CO_iaqi((int) covalue);
		int iaqi_o3_8h = AQIProcess.cal_O3_8h_iaqi(o3value);

		int a[] = { iaqi_pm25, iaqi_pm10, iaqi_so2, iaqi_no2, iaqi_co, iaqi_o3_8h };
		int index = AQIProcess.max(a);
		int aqi = a[index];
		String pollutStr = null;
		if (aqi <= 50 || aqi == 0)
			pollutStr = "无";
		else
			pollutStr = AQIProcess.pollutant[index];
		return pollutStr;
	}
	
	public static int getAqiLevel(int aqi) {
		int level[] = { 0, 50, 100, 150, 200, 300 };
		int result = 1;

		if (aqi > 300)
			return 6;

		for (int i = 1; i < level.length; i++) {
			if (aqi > level[i - 1] && aqi <= level[i]) {
				result = i;
				break;
			}
		}

		return result;
	}
	
	public static String getComprehensiveIndex(int pm10value, int pm25value, int so2value, int no2value, double covalue,
			int o3value) {
		double pm25index = Double.valueOf(df.format((double) pm10value / 35));
		double pm10index = Double.valueOf(df.format((double) pm25value / 70));
		double coindex = Double.valueOf(df.format((double) so2value / 4));
		double so2index = Double.valueOf(df.format((double) no2value / 60));
		double no2index = Double.valueOf(df.format((double) covalue / 40));
		double o3index = Double.valueOf(df.format((double) o3value / 160));

		double indexs = pm25index + pm10index + coindex + so2index + no2index + o3index;
		
		return df.format(indexs);
	}

	public static String getAqiLevelType(int aqiLevel) {
		String level[] = { "优", "良", "轻度污染", "中度污染", "重度污染", "严重污染" };

		String type = null;
		for (int i = 1; i <= 6; i++) {
			if (aqiLevel == i) {
				type = level[i - 1];
				break;
			}
		}

		return type;
	}

	public static int max(int[] a) {
		int temp = a[0];
		int index = 0;
		for (int i = 1; i < a.length; i++) {
			if (a[i] > temp) {
				temp = a[i];
				index = i;
			}
		}

		return index;
	}

	public static int cal_pm25_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int pm25[] = { 0, 35, 75, 115, 150, 250, 350, 500 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 500)
			return 500;

		int i;
		for (i = 1; i < pm25.length; i++) {
			if (cp >= pm25[i - 1] && cp < pm25[i]) {
				break;
			}
		}

		if (i == pm25.length)
			i = pm25.length - 1;
		BPH = pm25[i];
		BPL = pm25[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_pm10_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int pm10[] = { 0, 50, 150, 250, 350, 420, 500, 600 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 600)
			return 500;

		int i;
		for (i = 1; i < pm10.length; i++) {
			if (cp >= pm10[i - 1] && cp < pm10[i]) {
				break;
			}
		}

		if (i == pm10.length)
			i = pm10.length - 1;
		BPH = pm10[i];
		BPL = pm10[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_SO2_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200 };
		int so2[] = { 0, 150, 500, 650, 800 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 800)
			return 200;

		int i;
		for (i = 1; i < so2.length; i++) {
			if (cp >= so2[i - 1] && cp < so2[i]) {
				break;
			}
		}

		if (i == so2.length)
			i = so2.length - 1;
		BPH = so2[i];
		BPL = so2[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_NO2_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int no2[] = { 0, 100, 200, 700, 1200, 2340, 3090, 3840 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 3840)
			return 500;

		int i;
		for (i = 1; i < no2.length; i++) {
			if (cp >= no2[i - 1] && cp < no2[i]) {
				break;
			}
		}

		if (i == no2.length)
			i = no2.length - 1;
		BPH = no2[i];
		BPL = no2[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_CO_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int co[] = { 0, 5, 10, 35, 60, 90, 120, 150 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 150)
			return 500;

		int i;
		for (i = 1; i < co.length; i++) {
			if (cp >= co[i - 1] && cp < co[i]) {
				break;
			}
		}

		if (i == co.length)
			i = co.length - 1;
		BPH = co[i];
		BPL = co[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	/**
	 * 计算O3的分指数
	 * 
	 * @param cp
	 * @return
	 * @author dong
	 * @create_time 2015年8月13日下午7:29:03
	 */
	public static int cal_O3_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int o3[] = { 0, 160, 200, 300, 400, 800, 1000, 1200 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 1200)
			return 500;

		int i;
		for (i = 1; i < o3.length; i++) {
			if (cp >= o3[i - 1] && cp < o3[i]) {
				break;
			}
		}

		if (i == o3.length)
			i = o3.length - 1;
		BPH = o3[i];
		BPL = o3[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	/**
	 * 计算O3 8小时 的分指数
	 * 
	 * @param cp
	 * @return
	 */
	public static int cal_O3_8h_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300 };
		int o3[] = { 0, 100, 160, 215, 265, 800 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 1200)
			return 500;

		int i;
		for (i = 1; i < o3.length; i++) {
			if (cp >= o3[i - 1] && cp < o3[i]) {
				break;
			}
		}

		if (i == o3.length)
			i = o3.length - 1;
		BPH = o3[i];
		BPL = o3[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	/**
	 * 分指数计算公式
	 * 
	 * @param BPH
	 * @param BPL
	 * @param IAQIH
	 * @param IAQIL
	 * @param cp
	 * @return
	 * @author dong
	 * @create_time 2015年8月13日下午7:23:27
	 */
	public static int iaqi(int BPH, int BPL, int IAQIH, int IAQIL, int cp) {
		return ((IAQIH - IAQIL) * (cp - BPL) / (BPH - BPL) + IAQIL);
	}

	public static int cal_CO_24h_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int co[] = { 0, 2, 4, 14, 24, 36, 48, 60 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 150)
			return 500;

		int i;
		for (i = 1; i < co.length; i++) {
			if (cp >= co[i - 1] && cp < co[i]) {
				break;
			}
		}

		if (i == co.length)
			i = co.length - 1;
		BPH = co[i];
		BPL = co[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_NO2_24h_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int no2[] = { 0, 40, 80, 180, 280, 565, 750, 940 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 3840)
			return 500;

		int i;
		for (i = 1; i < no2.length; i++) {
			if (cp >= no2[i - 1] && cp < no2[i]) {
				break;
			}
		}

		if (i == no2.length)
			i = no2.length - 1;
		BPH = no2[i];
		BPL = no2[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

	public static int cal_SO2_24h_iaqi(int cp) {
		int iaqi[] = { 0, 50, 100, 150, 200, 300, 400, 500 };
		int so2[] = { 0, 50, 150, 475, 800, 1600, 2100, 2620 };
		int BPH = 0, BPL = 0, IAQIH = 0, IAQIL = 0;
		if (cp < 0)
			cp = 0;
		if (cp > 800)
			return 200;

		int i;
		for (i = 1; i < so2.length; i++) {
			if (cp >= so2[i - 1] && cp < so2[i]) {
				break;
			}
		}

		if (i == so2.length)
			i = so2.length - 1;
		BPH = so2[i];
		BPL = so2[i - 1];
		IAQIH = iaqi[i];
		IAQIL = iaqi[i - 1];

		return iaqi(BPH, BPL, IAQIH, IAQIL, cp);
	}

}
